package com.douglasalipio.service;

import java.io.IOException;

import com.douglasalipio.entity.PlacesAutocompleteList;
import com.douglasalipio.util.PlacesUtils;
import com.google.api.client.http.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport;

/**
 * 
 * @author Douglas
 * 
 */
public class PlacesService {

	private static final HttpTransport transport = new ApacheHttpTransport();

	public PlacesAutocompleteList searchAddress(String reference)
			throws IOException {

		HttpRequestFactory httpRequestFactory = ConnectionManager
				.createRequestFactory(transport);

		HttpRequest request = httpRequestFactory
				.buildGetRequest(new GenericUrl(PlacesUtils.PLACES_SEARCH_URL));

		request.url.put("input", reference);
		request.url.put("types", PlacesUtils.TYPE);
		request.url.put("region", PlacesUtils.REGION);
		request.url.put("language", PlacesUtils.LANGUAGE);
		request.url.put("sensor", PlacesUtils.SENSOR);
		request.url.put("key", PlacesUtils.PLACE_KEY);

		PlacesAutocompleteList places = request.execute().parseAs(
				PlacesAutocompleteList.class);

		return places;

	}

}
